package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrdersService ordersService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartService shoppingCartService;

    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page,
                                Integer pageSize,
                                Long number,
                                Date beginTime,
                                Date endTime) {//, Date beginTime,Date endTime

        /*
        *
        *     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
       *
       * */

        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.like(!StringUtils.isEmpty(number), "number", number);
        ordersQueryWrapper.between(!StringUtils.isEmpty(beginTime), "order_time", beginTime, endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.page(ordersPage, ordersQueryWrapper);
        return R.success(ordersPage);
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.page(ordersPage);
        List<OrdersDto> ordersDtos = ordersPage.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            List<OrderDetail> orderDetailList = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("order_id", orders.getId()));
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        Page<OrdersDto> ordersDtoPage = new Page<>();
        ordersDtoPage.setRecords(ordersDtos);
        ordersDtoPage.setTotal(ordersPage.getTotal());
        return R.success(ordersDtoPage);
    }

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @PostMapping("/again")
    public R again(@RequestBody Orders orders) {
        Long currentThread = BaseContext.getCurrentThread();
        List<OrderDetail> orderDetails = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("order_id", orders.getId()));

        List<ShoppingCart> shoppingCarts = orderDetails.stream().map(orderDetail -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail, shoppingCart, "id");//除了id都要复制
            shoppingCart.setUserId(currentThread);
            return shoppingCart;
        }).collect(Collectors.toList());

        log.info("购物车：{}",shoppingCarts);
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("下单成功");
    }

    @PutMapping
    public R update(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("修改成功");
    }
}
